<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/04/29
 * Time: 10:22
 */
namespace app\mini\controller\commons;
use app\common\Code;
use app\common\traits\InterfacesExceptionTrait;
use think\Controller;



/**
 * API模块 公共的控制器
 * Class Common
 */
class Base extends Controller
{
    use InterfacesExceptionTrait;

    protected static $http_code = 500; //http 错误代号

    /**
     * @param $handleResult
     * @param int $code
     * @param array $meta
     * @return \think\response\Json
     */
    protected function response($handleResult, array $meta = [], int $code = Code::DEFAULT_CODE)
    {
        $msg = 'success';
        $options = [];
        if ($handleResult == false) {
            if ($this->haveError()) {
                list($code, $msg) = $this->getFirstError();
                $httpCode = $this->getHttpCode();
            }
            $handleResult = [];
            $options = ['json_encode_param' => JSON_FORCE_OBJECT];
        }
        $data = [
            'code' => $code,
            'message' => $msg,
            'data' => $handleResult
        ];
        if(!empty($meta)) $data['meta'] = $meta;
        return json($data, $httpCode ?? $code,[],$options);
    }




}
